﻿// <copyright file="CommunityRecentComments.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-04-13</date>
// <summary>CommunityRecentComments Silverlight CLR class</summary>

namespace NYTimes
{
    using System.ComponentModel;
    using System.Globalization;
    using System.Text;
    using NYTimes.Core;

    /// <summary>
    /// New York Times Community Recent Comments API
    /// </summary>
    /// <remarks>
    /// <list type="table">
    /// <listheader>
    /// <term>Application Resource Key</term>
    /// <description>Key to add to application resources</description>
    /// </listheader>
    /// <item>
    /// <term>
    /// NYTCommunityKey
    /// </term>
    /// <description>Community API Key</description>
    /// </item>
    /// <item>
    /// <term>
    /// NYTCommunityQPS
    /// </term>
    /// <description>Rate Limiting (default 30 QPS)</description>
    /// </item>
    /// </list>
    /// </remarks>
    public class CommunityRecentComments : CommunityBase
    {
        #region Fields
        /// <summary>
        /// the result offset (25-based)
        /// </summary>
        private int offset;

        /// <summary>
        /// The maximum number of results to return
        /// </summary>
        private int maxResults = 25;

        /// <summary>
        /// force replies
        /// </summary>
        private bool forceReplies;

        /// <summary>
        /// the sort type
        /// </summary>
        private CommentSortType sort;

        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the CommunityRecentComments class.
        /// </summary>
        public CommunityRecentComments()
        {
            if (SampleData.IsInDesignTool)
            {
                var xml = SampleData.GetSampleData("CommunityRecentComments.xml");

                this.ParseXml(xml);
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets a value indicating whether to limit the results to comments that have been replied to
        /// </summary>
        [Description("True to get comments with replies, false to get comments that nave not been replied to.")]
        public bool ForceReplies 
        { 
            get
            {
                return this.forceReplies;
            }

            set
            {
                this.forceReplies = value;

                this.Offset = 0;
            }
        }

        /// <summary>
        /// Gets or sets the maximum number of results
        /// </summary>
        [Description("The maximum number of results to return.")]
        public int MaxResults
        {
            get
            {
                return this.maxResults;
            }

            set
            {
                if (this.maxResults != value)
                {
                    this.maxResults = value;

                    this.Offset = 0;
                }
            }
        }

        /// <summary>
        /// Gets or sets the sort order of the results
        /// </summary>
        [Description("Gets or sets the sort order of the results")]
        public CommentSortType Sort
        {
            get
            {
                return this.sort;
            }

            set
            {
                if (this.sort != value)
                {
                    this.sort = value;

                    this.Offset = 0;
                }
            }
        }

        /// <summary>
        /// Gets the offset index (25-based)
        /// </summary>
        public int Offset
        {
            get
            {
                return this.offset;
            }

            private set
            {
                this.offset = value;

                this.FetchResults(this.offset == 0);
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Generate the URI string
        /// </summary>
        /// <returns>the output URI string</returns>
        public override string GenerateUriFragment()
        {
            var builder = new StringBuilder();

            builder.Append("recent.xml?");

            if (this.ForceReplies)
            {
                builder.Append("force-replies=1");
            }
            else
            {
                builder.Append("force-replies=0");
            }

            builder.Append("&");

            AppendSortType(builder, this.Sort);

            if (this.Offset > 0)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, "&offset={0}", this.Offset);
            }

            return builder.ToString();
        }

        /// <summary>
        /// Fetch the next set
        /// </summary>
        /// <returns>true if the results are done loading, false otherwise.</returns>
        public override bool FetchNextSet()
        {
            if (this.Results.Count < this.MaxResults && this.Results.Count < this.TotalComments)
            {
                this.Offset += 25;

                return false;
            }

            return true;
        }
        #endregion
    }
}
